<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/29/029
 * Time: 9:32
 */

namespace app\api\model;

class Category extends BaseModel {

    protected $hidden = ['update_time','delete_time', 'create_time', 'pid', 'level', 'status', 'sort', 'is_shop', 'is_texu', 'is_customization', 'icon', 'index_cover', 'delete', 'brand'];

    public function getImageAttr($value){
        return get_img_url($value);
    }

    /**
     * 获取所有正常的分类
     * @return array|\PDOStatement|string|\think\Collection
     */
    public static function get_category_list() {

        $cate = self::where(function($query) {
                    $query->where('status', '=', 1)->where('id', '>', 143);
                })->order('sort desc')->select();

        return $cate;
    }

    /**
     * 根据key获取分类数据
     * @param $key
     * @return array|\PDOStatement|string|\think\Collection
     */
    public static function getCategoryByKey($key) {
        $where = [
            [$key, '=', 1],
            ['level', '=', 1],
            ['pid', '=', 0],
            ['status', '=', 1],
            ['delete', '=', 0],
            ['id', 'gt', 143],
        ];

        $data = self::where($where)->order('sort desc, id desc')->select();

        if ($key != 'is_customization') {
            foreach ($data as $k => &$v) {
                if ($v->brand) {
                    $brand = Brand::all($v->brand);
                    $v->brand = $brand;
                }
            }
        }
        return $data;
    }
}